#include "font.h"
#include <string.h> // For strlen
#include <stdlib.h> // For itoa if not using sprintf, but sprintf is better.
#include <stdio.h>  // For sprintf

// 简单的 8x8 位图字体 (ASCII 32-126)
// 每个字符 8 字节，每字节代表一行像素
// 这里的字体数据现在扩展到包含数字、大写字母、小写字母及常用符号。
#define MIN_FONT_ASCII 32
#define MAX_FONT_ASCII 122 // 现在包含到小写字母 'z' (ASCII 122)

static const u8 font8x8[] = {
    // ASCII 32 (Space)
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    // ASCII 33 (!)
    0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x18, 0x00,
    // ASCII 34 (")
    0x6C, 0x6C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    // ASCII 35 (#)
    0x00, 0x66, 0x66, 0x3C, 0x66, 0x66, 0x00, 0x00,
    // ASCII 36 ($)
    0x18, 0x7E, 0x18, 0x3C, 0x06, 0x7E, 0x18, 0x00,
    // ASCII 37 (%)
    0x66, 0x66, 0x0C, 0x18, 0x30, 0x66, 0x66, 0x00,
    // ASCII 38 (&)
    0x38, 0x6C, 0x38, 0x1C, 0x36, 0x6C, 0x38, 0x00,
    // ASCII 39 (')
    0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    // ASCII 40 (()
    0x0C, 0x18, 0x30, 0x30, 0x30, 0x18, 0x0C, 0x00,
    // ASCII 41 ())
    0x30, 0x18, 0x0C, 0x0C, 0x0C, 0x18, 0x30, 0x00,
    // ASCII 42 (*)
    0x00, 0x66, 0x3C, 0x18, 0x3C, 0x66, 0x00, 0x00,
    // ASCII 43 (+)
    0x00, 0x18, 0x18, 0x7E, 0x18, 0x18, 0x00, 0x00,
    // ASCII 44 (,)
    0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x30, 0x00,
    // ASCII 45 (-)
    0x00, 0x00, 0x00, 0x7E, 0x00, 0x00, 0x00, 0x00,
    // ASCII 46 (.)
    0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x00,
    // ASCII 47 (/)
    0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x00, 0x00,
    // ASCII 48 (0)
    0x3C, 0x42, 0x42, 0x42, 0x42, 0x42, 0x3C, 0x00,
    // ASCII 49 (1)
    0x18, 0x38, 0x18, 0x18, 0x18, 0x18, 0x7E, 0x00,
    // ASCII 50 (2)
    0x3C, 0x42, 0x02, 0x04, 0x08, 0x10, 0x7E, 0x00,
    // ASCII 51 (3)
    0x7E, 0x02, 0x04, 0x0C, 0x02, 0x42, 0x3C, 0x00,
    // ASCII 52 (4)
    0x0C, 0x14, 0x24, 0x44, 0x7E, 0x04, 0x04, 0x00,
    // ASCII 53 (5)
    0x7E, 0x40, 0x40, 0x3C, 0x02, 0x02, 0x3C, 0x00,
    // ASCII 54 (6)
    0x3C, 0x40, 0x40, 0x7C, 0x42, 0x42, 0x3C, 0x00,
    // ASCII 55 (7)
    0x7E, 0x02, 0x04, 0x08, 0x10, 0x20, 0x20, 0x00,
    // ASCII 56 (8)
    0x3C, 0x42, 0x42, 0x3C, 0x42, 0x42, 0x3C, 0x00,
    // ASCII 57 (9)
    0x3C, 0x42, 0x42, 0x3E, 0x02, 0x02, 0x3C, 0x00,
    // ASCII 58 (:)
    0x00, 0x18, 0x18, 0x00, 0x00, 0x18, 0x18, 0x00,
    // ASCII 59 (;)
    0x00, 0x18, 0x18, 0x00, 0x00, 0x18, 0x18, 0x30,
    // ASCII 60 (<)
    0x08, 0x10, 0x20, 0x40, 0x20, 0x10, 0x08, 0x00,
    // ASCII 61 (=)
    0x00, 0x00, 0x7E, 0x00, 0x7E, 0x00, 0x00, 0x00,
    // ASCII 62 (>)
    0x40, 0x20, 0x10, 0x08, 0x10, 0x20, 0x40, 0x00,
    // ASCII 63 (?)
    0x3C, 0x42, 0x04, 0x08, 0x10, 0x00, 0x18, 0x00,
    // ASCII 64 (@)
    0x3C, 0x42, 0x5A, 0x5A, 0x5A, 0x40, 0x3C, 0x00,
    // ASCII 65 (A)
    0x18, 0x3C, 0x66, 0x66, 0x7E, 0x66, 0x66, 0x00,
    // ASCII 66 (B)
    0x7C, 0x66, 0x66, 0x7C, 0x66, 0x66, 0x7C, 0x00,
    // ASCII 67 (C)
    0x3C, 0x66, 0x40, 0x40, 0x40, 0x66, 0x3C, 0x00,
    // ASCII 68 (D) 
    0x78, 0x6C, 0x66, 0x66, 0x66, 0x6C, 0x78, 0x00,
    // ASCII 69 (E)
    0x7E, 0x64, 0x64, 0x7C, 0x64, 0x64, 0x7E, 0x00,
    // ASCII 70 (F)
    0x7E, 0x60, 0x60, 0x7C, 0x60, 0x60, 0x60, 0x00,
    // ASCII 71 (G)
    0x3C, 0x66, 0x40, 0x5E, 0x42, 0x66, 0x3C, 0x00,
    // ASCII 72 (H)
    0x66, 0x66, 0x66, 0x7E, 0x66, 0x66, 0x66, 0x00,
    // ASCII 73 (I)
    0x3C, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3C, 0x00,
    // ASCII 74 (J)
    0x3E, 0x08, 0x08, 0x08, 0x08, 0x48, 0x30, 0x00,
    // ASCII 75 (K)
    0x66, 0x6C, 0x78, 0x70, 0x78, 0x6C, 0x66, 0x00,
    // ASCII 76 (L)
    0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x7E, 0x00,
    // ASCII 77 (M)
    0xC6, 0xEE, 0xFE, 0xDA, 0xC6, 0xC6, 0xC6, 0x00,
    // ASCII 78 (N)
    0xC6, 0xE6, 0xF6, 0xDE, 0xCE, 0xC6, 0xC6, 0x00,
    // ASCII 79 (O)
    0x3C, 0x42, 0x42, 0x42, 0x42, 0x42, 0x3C, 0x00,
    // ASCII 80 (P)
    0x7C, 0x66, 0x66, 0x7C, 0x60, 0x60, 0x60, 0x00,
    // ASCII 81 (Q)
    0x3C, 0x66, 0x66, 0x66, 0x66, 0x6C, 0x3A, 0x00,
    // ASCII 82 (R)
    0x7C, 0x66, 0x66, 0x7C, 0x70, 0x68, 0x66, 0x00,
    // ASCII 83 (S)
    0x3E, 0x60, 0x60, 0x3C, 0x06, 0x06, 0x7C, 0x00,
    // ASCII 84 (T)
    0x7E, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00,
    // ASCII 85 (U)
    0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x3C, 0x00,
    // ASCII 86 (V)
    0x66, 0x66, 0x66, 0x66, 0x3C, 0x18, 0x00, 0x00,
    // ASCII 87 (W)
    0xC6, 0xC6, 0xC6, 0xDA, 0xFE, 0xEE, 0xC6, 0x00,
    // ASCII 88 (X)
    0x66, 0x66, 0x3C, 0x18, 0x3C, 0x66, 0x66, 0x00,
    // ASCII 89 (Y)
    0x66, 0x66, 0x3C, 0x18, 0x18, 0x18, 0x18, 0x00,
    // ASCII 90 (Z)
    0x7E, 0x02, 0x04, 0x08, 0x10, 0x20, 0x7E, 0x00,
    //  (ASCII 91 到 122) ---
    // ASCII 91 ([)
    0x3C, 0x30, 0x30, 0x30, 0x30, 0x30, 0x3C, 0x00,
    // ASCII 92 (\)
    0x06, 0x0C, 0x18, 0x30, 0x60, 0x40, 0x00, 0x00,
    // ASCII 93 (])
    0x3C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x3C, 0x00,
    // ASCII 94 (^)
    0x18, 0x3C, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00,
    // ASCII 95 (_)
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7E,
    // ASCII 96 (`)
    0x30, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    // ASCII 97 (a)
    0x00, 0x00, 0x3C, 0x66, 0x7E, 0x66, 0x66, 0x00,
    // ASCII 98 (b)
    0x40, 0x40, 0x40, 0x7C, 0x66, 0x66, 0x7C, 0x00,
    // ASCII 99 (c)
    0x00, 0x00, 0x3C, 0x66, 0x40, 0x66, 0x3C, 0x00,
    // ASCII 100 (d)
    0x02, 0x02, 0x02, 0x3E, 0x66, 0x66, 0x3E, 0x00,
    // ASCII 101 (e)
    0x00, 0x00, 0x3C, 0x66, 0x7E, 0x60, 0x3C, 0x00,
    // ASCII 102 (f)
    0x0C, 0x1A, 0x10, 0x3C, 0x10, 0x10, 0x10, 0x00,
    // ASCII 103 (g)
    0x00, 0x00, 0x3E, 0x66, 0x66, 0x3E, 0x06, 0x7C,
    // ASCII 104 (h)
    0x40, 0x40, 0x40, 0x7C, 0x64, 0x64, 0x64, 0x00,
    // ASCII 105 (i)
    0x18, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00,
    // ASCII 106 (j)
    0x0C, 0x00, 0x0C, 0x0C, 0x0C, 0x0C, 0x4C, 0x30,
    // ASCII 107 (k)
    0x40, 0x40, 0x40, 0x6C, 0x30, 0x6C, 0x40, 0x00,
    // ASCII 108 (l)
    0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00,
    // ASCII 109 (m)
    0x00, 0x00, 0xEE, 0xAA, 0xAA, 0xAA, 0xAA, 0x00,
    // ASCII 110 (n)
    0x00, 0x00, 0x7C, 0x64, 0x64, 0x64, 0x64, 0x00,
    // ASCII 111 (o)
    0x00, 0x00, 0x3C, 0x42, 0x42, 0x42, 0x3C, 0x00,
    // ASCII 112 (p)
    0x00, 0x00, 0x7C, 0x66, 0x66, 0x7C, 0x60, 0x60,
    // ASCII 113 (q)
    0x00, 0x00, 0x3E, 0x66, 0x66, 0x3E, 0x02, 0x02,
    // ASCII 114 (r)
    0x00, 0x00, 0x60, 0x70, 0x60, 0x60, 0x60, 0x00,
    // ASCII 115 (s)
    0x00, 0x00, 0x3E, 0x60, 0x3C, 0x06, 0x7C, 0x00,
    // ASCII 116 (t)
    0x00, 0x18, 0x18, 0x7E, 0x18, 0x18, 0x38, 0x00,
    // ASCII 117 (u)
    0x00, 0x00, 0x66, 0x66, 0x66, 0x66, 0x3E, 0x00,
    // ASCII 118 (v)
    0x00, 0x00, 0x66, 0x66, 0x3C, 0x18, 0x00, 0x00,
    // ASCII 119 (w)
    0x00, 0x00, 0x66, 0x66, 0x5A, 0x5A, 0x3C, 0x00,
    // ASCII 120 (x)
    0x00, 0x00, 0x66, 0x3C, 0x18, 0x3C, 0x66, 0x00,
    // ASCII 121 (y)
    0x00, 0x00, 0x66, 0x66, 0x3C, 0x18, 0x18, 0x30,
    // ASCII 122 (z)
    0x00, 0x00, 0x7E, 0x20, 0x10, 0x08, 0x04, 0x7E
};

// 绘制一个像素 (3Bpp BGR 格式)
static inline void DrawPixel(u8* frameBuffer, u32 fb_width, u32 fb_height, int x, int y, u8 r, u8 g, u8 b) {
    if (x >= 0 && (u32)x < fb_width && y >= 0 && (u32)y < fb_height) {
        u32 offset = (y * fb_width + x) * 3;
        frameBuffer[offset + 0] = b; // Blue
        frameBuffer[offset + 1] = g; // Green
        frameBuffer[offset + 2] = r; // Red
    }
}

// 绘制单个字符
void DrawChar(u8* frameBuffer, u32 fb_width, u32 fb_height, int x, int y, char character, u8 r, u8 g, u8 b) {
    // 检查字符是否在我们定义的字体范围内
    if (character < MIN_FONT_ASCII || character > MAX_FONT_ASCII) {
        return; // 超出定义的字符范围，不绘制
    }
    
    u32 font_index = character - MIN_FONT_ASCII;
    
    // 理论上这个检查不会触发，因为 character 已经通过 MIN/MAX_FONT_ASCII 检查。
    // 但作为安全措施保留。
    if (font_index * FONT_HEIGHT >= sizeof(font8x8)) {
        return; 
    }

    const u8* char_data = &font8x8[font_index * FONT_HEIGHT];

    for (int row = 0; row < FONT_HEIGHT; ++row) {
        u8 line_data = char_data[row];
        for (int col = 0; col < FONT_WIDTH; ++col) {
            // 从高位到低位读取 (MSB-first, Left-to-Right)
            if ((line_data >> (FONT_WIDTH - 1 - col)) & 0x01) { 
                DrawPixel(frameBuffer, fb_width, fb_height, x + col, y + row, r, g, b);
            }
        }
    }
}

// 绘制字符串
void DrawString(u8* frameBuffer, u32 fb_width, u32 fb_height, int x, int y, const char* text, u8 r, u8 g, u8 b) {
    int current_x = x;
    for (int i = 0; text[i] != '\0'; ++i) {
        DrawChar(frameBuffer, fb_width, fb_height, current_x, y, text[i], r, g, b);
        current_x += FONT_WIDTH; // 每个字符占 FONT_WIDTH 宽度
    }
}

// 绘制数字
void DrawNumber(u8* frameBuffer, u32 fb_width, u32 fb_height, int x, int y, int number, u8 r, u8 g, u8 b) {
    char buffer[16]; // 足够存放一个整数及负号
    sprintf(buffer, "%d", number);
    DrawString(frameBuffer, fb_width, fb_height, x, y, buffer, r, g, b);
}

